/*
 * @lc app=leetcode.cn id=235 lang=c
 *
 * [235] 二叉搜索树的最近公共祖先
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */

//  struct TreeNode {
//      int val;
//      struct TreeNode *left;
//      struct TreeNode *right;
//  };

struct TreeNode* lowestCommonAncestor(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q) {
    if (root == NULL || root->val == p->val || root->val == q->val) {
        return root;
    }
    if (root->val > p->val && root->val < q->val)
    {
        return root;
    } 
    if (root->val < p->val && root->val > q->val)
    {
        return root;
    }
    return lowestCommonAncestor(p->val < root->val ? root->left:root->right, p, q);
}
// @lc code=end

